import { createApp } from "vue";
import TheLoading from "@/components/TheLoading";

let mountNode = null;
let Instance = null;

function close() {
  Instance.unmount(mountNode);
  document.body.removeChild(mountNode);
}

const createLoading = (option = {}) => {
  const dom = document.body.querySelector(".my-loadng");
  if (!dom) {
    mountNode = document.createElement("div");
    mountNode.className = "my-loading";
    document.body.appendChild(mountNode);
  } else {
    Instance.unmount();
  }
  // const mountNode = document.createElement("div");
  Instance = createApp(TheLoading, {
    show: true,
    ...option,
    close,
  });

  Instance.mount(mountNode);
};

export default {
  show: createLoading,
  close,
};
